package base.heart;

import base.model.NettyMessage;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by wuhui on 2016/8/4.
 */
@Slf4j
public class HeartBeatRespHandler extends ChannelHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info(msg.toString());
        NettyMessage nettyMessage = (NettyMessage) msg;
        if(nettyMessage.getHeader()!=null&&nettyMessage.getHeader().getType()==2){
            NettyMessage heartbeat = buildHeatBeat();
            log.info("发送心跳包");
            ctx.writeAndFlush(heartbeat);
        }else {
            ctx.fireChannelRead(msg);
        }
    }
    private NettyMessage buildHeatBeat(){
        return  NettyMessage.newNettyMessage((byte)2);
    }

}
